package serializable;

import java.io.*;

public class person implements Serializable {

    // 为该类指定一个 serialVersionUID变量值
    private  static final long serialVersionUID = 1L;
    // 声明变量
    private String name;
    transient private int id;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ",name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) throws Exception{

        // 创建对象
        person p = new person();
        p.setAge(12);
        p.setId(01);
        p.setName("li");
        System.out.println(p);

        //Write Obj to File
        try {
            // 创建序列化对象
            ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("src/serializable/tempFile"));
            // 写入
            oo.writeObject(p);

        }catch (IOException e) {
            e.printStackTrace();
        }

        //Read Obj from File
        File file = new File("src/serializable/tempFile");
        try {
            // 创建反序列化对象
            ObjectInputStream oi = new ObjectInputStream(new FileInputStream(file));
            person ps = (person) oi.readObject();
            System.out.println(ps);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
